package com.example.demo;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import android.widget.Toast;

import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import java.util.ArrayList;
import java.util.List;

// HomeFragment.java
@SuppressLint("MissingInflatedId")
public class HomeFragment extends Fragment {
    //
    private RecyclerView recyclerView;
    private UserAdapter adapter;
    private List<User> userList = new ArrayList<>();

    //
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_layout, container, false);

        // 设置页面标识和背景色
        TextView textView = view.findViewById(R.id.viewPagerFragmentTv);
        textView.setText("首页\n\n这是第一个Fragment");
        view.setBackgroundColor(0xFFFFCCCC); // 浅红色背景
        //
        initData();
        initRecyclerView( view);
        //

        return view;
    }

    private void initData() {
        // 添加模拟数据
        userList.add(new User("张三", "zhangsan@email.com", R.drawable.ic_launcher_foreground));
        userList.add(new User("李四", "lisi@email.com", R.drawable.ic_launcher_foreground));
        userList.add(new User("王五", "wangwu@email.com", R.drawable.ic_launcher_foreground));
        userList.add(new User("赵六", "zhaoliu@email.com", R.drawable.ic_launcher_foreground));
        userList.add(new User("钱七", "qianqi@email.com", R.drawable.ic_launcher_foreground));

        // 可以继续添加更多数据...
        for (int i = 6; i <= 20; i++) {
            userList.add(new User("用户" + i, "user" + i + "@email.com", R.mipmap.ic_launcher));
        }
    }

    private void initRecyclerView(View view) {
        recyclerView = view.findViewById(R.id.fragmentRecyclerView);
        Log.d("errerrerr", String.valueOf(recyclerView));
        // 设置布局管理器
        try {
            LinearLayoutManager layoutManager = new LinearLayoutManager(getContext());
            recyclerView.setLayoutManager(layoutManager);
        } catch (Exception e) {
            Log.d("errerrerr", "initRecyclerView: ");
        }


        // 创建适配器并设置
        adapter = new UserAdapter(userList, new UserAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(int position) {
                User user = userList.get(position);
                Toast.makeText(getContext(), "点击: " + user.getName(), Toast.LENGTH_SHORT).show();
            }

            @Override
            public void onItemLongClick(int position) {
                User user = userList.get(position);
                Toast.makeText(getContext(), "长按: " + user.getName(), Toast.LENGTH_SHORT).show();
            }
        });

        recyclerView.setAdapter(adapter);
    }
}
